package com.example.idachuappone.index.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import com.example.idachuappone.R;
import com.example.idachuappone.utils.ImgCallBack;
import com.example.idachuappone.utils.Utils;
import java.util.ArrayList;
import java.util.List;

public class ImgsAdapter extends BaseAdapter {
    public Bitmap[] bitmaps;
    Context context;
    List<String> data;
    List<View> holderlist;
    private int index;
    OnItemClickClass onItemClickClass;
    private int sum;
    Utils util;
    int width;

    class Holder {
        CheckBox checkBox;
        ImageView imageView;

        Holder() {
        }
    }

    public static interface OnItemClickClass {
        void OnItemClick(View view, int i, CheckBox checkBox);
    }

    class OnPhotoClick implements OnClickListener {
        CheckBox checkBox;
        int position;

        public OnPhotoClick(int position, CheckBox checkBox) {
            this.position = position;
            this.checkBox = checkBox;
        }

        public void onClick(View v) {
            if (ImgsAdapter.this.data != null && ImgsAdapter.this.onItemClickClass != null) {
                ImgsAdapter.this.onItemClickClass.OnItemClick(v, this.position, this.checkBox);
            }
        }
    }

    public class ImgClallBackLisner implements ImgCallBack {
        int num;

        public ImgClallBackLisner(int num) {
            this.num = num;
        }

        public void resultImgCall(ImageView imageView, Bitmap bitmap) {
            ImgsAdapter.this.bitmaps[this.num] = bitmap;
            imageView.setImageBitmap(bitmap);
        }
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public void setSum(int sum) {
        this.sum = sum;
    }

    public ImgsAdapter(Context context, List<String> data, OnItemClickClass onItemClickClass) {
        this.index = -1;
        this.context = context;
        this.data = data;
        this.onItemClickClass = onItemClickClass;
        this.bitmaps = new Bitmap[data.size()];
        this.util = new Utils(context);
        this.holderlist = new ArrayList();
    }

    public int getCount() {
        return this.data.size();
    }

    public Object getItem(int arg0) {
        return this.data.get(arg0);
    }

    public long getItemId(int arg0) {
        return (long) arg0;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public View getView(int arg0, View arg1, ViewGroup arg2) {
        Holder holder;
        if (arg0 == this.index || arg0 <= this.index) {
            holder = (Holder) ((View) this.holderlist.get(arg0)).getTag();
            arg1 = this.holderlist.get(arg0);
        } else {
            this.index = arg0;
            arg1 = LayoutInflater.from(this.context).inflate(R.layout.imgsitem, null);
            holder = new Holder();
            holder.imageView = (ImageView) arg1.findViewById(R.id.imageView1);
            holder.checkBox = (CheckBox) arg1.findViewById(R.id.checkBox1);
            arg1.setTag(holder);
            this.holderlist.add(arg1);
        }
        if (this.bitmaps[arg0] == null) {
            this.util.imgExcute(holder.imageView, new ImgClallBackLisner(arg0), (String) this.data.get(arg0));
        } else {
            holder.imageView.setImageBitmap(this.bitmaps[arg0]);
        }
        if (this.sum != 5) {
            arg1.setEnabled(true);
        } else if (!holder.checkBox.isChecked()) {
            arg1.setEnabled(false);
        }
        arg1.setOnClickListener(new OnPhotoClick(arg0, holder.checkBox));
        return arg1;
    }
}
